package com.egao.common.system.biz.impl;

import com.egao.appo.entity.Introduction;
import com.egao.appo.entity.PayRecord;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.Menu;
import com.egao.common.system.entity.Role;
import com.egao.common.system.entity.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-05-27 15:04:13
 * @version ：1.0
 */
@Repository
public class UserBizImpl extends BaseDao implements UserBiz {


	@Override
	public List<User> listAll2(User user, User loginUser) {
		RowMapper<User> rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id, u.username");
		sql.append(",u.nickname,u.avatar,u.sex,u.phone,u.birthday,u.type");
		sql.append(",u.country,u.province,u.city");
		sql.append(",u.create_time,u.update_time");
		sql.append(" from sys_user u ");
		sql.append(" where u.user_id=:user_id ");
		map.put("user_id", user.getUserId()+"");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and u.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(user)){
			if(Utils.isNotEmpty(user.getNickname())){
				sql.append(" and instr(u.nickname,:nickname)>0 ");
				map.put("nickname", user.getNickname());
			}
			if(Utils.isNotEmpty(user.getType())){
				sql.append(" and u.type=:type ");
				map.put("type", user.getType());
			}
			sql.append(" order by i.create_time desc");
		}
		sql.append(" limit 100 ");
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public User getById2(User user) {
		RowMapper<User> rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id, u.username");
		sql.append(",u.nickname,u.avatar,u.sex,u.phone,u.birthday,u.type");
		sql.append(",u.country,u.province,u.city");
		sql.append(",u.create_time,u.update_time");
		sql.append(" from sys_user u ");
		sql.append(" where  u.user_id=:user_id ");
		map.put("user_id", user.getUserId()+"");
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public User getByGzhId(User user) {
		RowMapper<User> rm = new BeanPropertyRowMapper<User>(User.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select u.user_id, u.username");
		sql.append(",u.nickname,u.avatar,u.sex,u.phone,u.birthday,u.type");
		sql.append(",u.country,u.province,u.city");
		sql.append(",u.gzh_id");
		sql.append(",u.create_time,u.update_time");
		sql.append(" from sys_user u ");
		sql.append(" where  u.gzh_id=:gzh_id ");
		map.put("gzh_id", user.getGzhId()+"");
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}


	@Override
	public Integer save2(User user) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_user (");
		sql.append("organization_id,username,password");
		sql.append(",password_origin,nickname");
		sql.append(",sex,gzh_id");
		sql.append(",phone,type,state");
		sql.append(") values ( ");
		sql.append(":organization_id,:username,:password");
		sql.append(",:password_origin,:nickname");
		sql.append(",:sex,:gzh_id");
		sql.append(",:phone,:type,:state");
		sql.append(")");
		map.put("organization_id", user.getOrganizationId() + "");
		map.put("username", user.getUsername());
		map.put("password", user.getPassword());
		map.put("password_origin", user.getPasswordOrigin());
		map.put("nickname", user.getNickname());
		map.put("sex", user.getSex()+"");
		map.put("gzh_id", user.getGzhId());
		map.put("phone", user.getPhone());
		map.put("type", user.getType());
		map.put("state", user.getState() + "");
		getMapParameter(sql, map);
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update2(User user) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sys_user set ");
		sql.append("nickname=:nickname");
		sql.append(",country=:country,province=:province");
		sql.append(",sex=:sex,city=:city,avatar=:avatar");
		sql.append(",phone=:phone");
		sql.append(" where user_id=:user_id ");
		map.put("nickname", user.getNickname());
		map.put("country", user.getCountry());
		map.put("province", user.getProvince());
		map.put("sex", user.getSex()+"");
		map.put("city", user.getCity());
		map.put("avatar", user.getAvatar());
		map.put("phone", user.getPhone());
		map.put("user_id", user.getUserId()+"");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		getMapParameter(sql, map);
		return count > 0;
	}

	@Override
	public boolean ModifyPassword(User user) {
			Map<String, String> map  = new HashMap<String, String>();
			StringBuffer sql =new StringBuffer("update sys_user set ");
			sql.append("password_origin=:password_origin,password=:password");
			sql.append(",update_time=now() where user_id=:user_id ");
			map.put("password_origin", user.getPasswordOrigin());
			map.put("password", user.getPassword());
			map.put("user_id", user.getUserId()+"");
			getMapParameter(sql, map);
			int count = jdbcTemplate.update(sql.toString(), map);
			return count > 0;
	}


	@Override
	public boolean saveUserRole(User user) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sys_user_role (");
		sql.append("user_id,role_id) values ( ");
		sql.append(":user_id,:role_id");
		sql.append(")");
		map.put("user_id", user.getUserId() + "");
		map.put("role_id", user.getRoleId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

}
